package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Mr.Shang
 * @date: 2021/10/27 17:19
 * @version: 1.0
 * @description: 客户管理
 */
@RestController
@RequestMapping("/sys/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/table")
    public TableData<Customer> getAllCustomer(@RequestParam(value = "page", required = false, defaultValue = "1") int pageIndex,
                                              @RequestParam(value = "limit", required = false, defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Customer> allCustomer = customerService.getAllCustomer();
        return new TableData<>(allCustomer.size(), allCustomer);
    }

    @RequestMapping("/del")
    public AjaxMessage deleteCustomer(int[] ids) {
        for (int id : ids) {
            customerService.deleteCustomer(id);
        }
        return new AjaxMessage(true,"删除成功");
    }

    @RequestMapping("/update")
    public AjaxMessage updateCustomer(Customer customer) {
        customerService.updateCustomer(customer);
        return new AjaxMessage(true,"更新成功");
    }

    @RequestMapping("/add")
    public AjaxMessage addCustomer(Customer customer) {
        customerService.addCustomer(customer);
        return new AjaxMessage(true,"添加成功");
    }

    @RequestMapping("/tree")
    public List<Customer> getAllCustomerId(){
        List<Customer> allIdAndNickName = customerService.getAllIdAndNickName();
        return allIdAndNickName;
    }
}
